package com.xxw.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xxw.yygh.client.DictFeignClient;
import com.xxw.yygh.common.exception.YyghException;
import com.xxw.yygh.enums.DictEnum;
import com.xxw.yygh.hosp.dao.HospitalSetDao;
import com.xxw.yygh.hosp.repository.HospitalRepository;
import com.xxw.yygh.hosp.service.HospitalService;
import com.xxw.yygh.model.hosp.Hospital;
import com.xxw.yygh.model.hosp.HospitalSet;
import com.xxw.yygh.vo.hosp.HospitalQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Resource
    private HospitalRepository hospitalRepository;

    @Resource
    private HospitalSetDao hospitalSetDao;

    @Resource
    private DictFeignClient dictFeignClient;

    @Override
    public void saveHospital(Map<String,Object> resultMap) {

        String s = JSONObject.toJSONString(resultMap);
        Hospital hospital = JSONObject.parseObject(s, Hospital.class);

        //判断数据是否已经存在
        String hoscode = hospital.getHoscode();
        Hospital collection=hospitalRepository.findByHoscode(hoscode);
        if (collection == null) {
            //不存在，新增
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }else{
            //存在，修改
            hospital.setStatus(collection.getStatus());
            hospital.setCreateTime(collection.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(collection.getIsDeleted());

            hospital.setId(collection.getId());
            hospitalRepository.save(hospital);
        }

    }

    @Override
    public String getSignKeyWithHoscode(String requestHoscode) {
        QueryWrapper<HospitalSet> hospitalSetQueryWrapper = new QueryWrapper<>();
        hospitalSetQueryWrapper.eq("hoscode", requestHoscode);
        HospitalSet hospitalSet = hospitalSetDao.selectOne(hospitalSetQueryWrapper);
        if (hospitalSet == null) {
            throw new YyghException(20001,"该医院不存在");
        }
        return hospitalSet.getSignKey();
    }

    @Override
    public Hospital getHospitalByHoscode(String hoscode) {
        return hospitalRepository.findByHoscode(hoscode);
    }

    @Override
    public Page<Hospital> getHospitalPage(Integer pageNum, Integer pageSize, HospitalQueryVo hospitalQueryVo) {
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
//        if (!StringUtils.isEmpty(hospitalQueryVo.getHosname())) {
//            hospital.setHosname(hospitalQueryVo.getHosname());
//        }
//        if (!StringUtils.isEmpty(hospitalQueryVo.getProvinceCode())) {
//            hospital.setProvinceCode(hospitalQueryVo.getProvinceCode());
//        }
//        if (!StringUtils.isEmpty(hospitalQueryVo.getCityCode())) {
//            hospital.setCityCode(hospitalQueryVo.getCityCode());
//        }
//        if (!StringUtils.isEmpty(hospitalQueryVo.getHostype())) {
//            hospital.setHostype(hospitalQueryVo.getHostype());
//        }
//        if (!StringUtils.isEmpty(hospitalQueryVo.getDistrictCode())) {
//            hospital.setDistrictCode(hospitalQueryVo.getDistrictCode());
//        }

        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("hosname",ExampleMatcher.GenericPropertyMatchers.contains())
                .withIgnoreCase(true);

        Example<Hospital> of = Example.of(hospital,matcher);
        PageRequest pageRequest = PageRequest.of(pageNum - 1, pageSize, Sort.by("createTime").ascending());
        Page<Hospital> page = hospitalRepository.findAll(of, pageRequest);

        page.getContent().stream().forEach(item->{
            this.packageHospital(item);
        });

        return page;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        if(status == 0 || status == 1){
            Hospital hospital = hospitalRepository.findById(id).get();
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Hospital detail(String id) {
        Hospital hospital = hospitalRepository.findById(id).get();
        this.packageHospital(hospital);

        return hospital;
    }

    @Override
    public List<Hospital> findByNameLike(String name) {
        return hospitalRepository.findByHosnameLike(name);
    }

    @Override
    public Hospital getHospitalDetail(String hoscode) {
        Hospital hospital = hospitalRepository.findByHoscode(hoscode);
        this.packageHospital(hospital);

        return hospital;
    }

    private void packageHospital(Hospital hospital){
        String hostype = hospital.getHostype();
        String provinceCode = hospital.getProvinceCode();
        String cityCode = hospital.getCityCode();
        String districtCode = hospital.getDistrictCode();

        String provinceAddress = dictFeignClient.getNameByValue(Long.parseLong(provinceCode));
        String cityAddress = dictFeignClient.getNameByValue(Long.parseLong(cityCode));
        String districtAddress = dictFeignClient.getNameByValue(Long.parseLong(districtCode));

        String level = dictFeignClient.getNameByDictCodeAndValue(DictEnum.HOSTYPE.getDictCode(), Long.parseLong(hostype));

        hospital.getParam().put("hostypeString",level);
        hospital.getParam().put("fullAddress",provinceAddress+cityAddress+districtAddress + hospital.getAddress());
    }
}
